/**
 * All rights Reserved, Designed By 姑苏区政府大数据中心
 * @Copyright: 2019 姑苏区政府大数据中心. All rights reserved.
 * 注意：本内容仅限于姑苏区政府大数据中心内部传阅，禁止外泄以及用于其他的商业目
 */
package com.dci.service.impl.project;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.dci.context.Config;
import com.dci.dao.pms.project.ProjectInfoMapper;
import com.dci.dao.pms.project.ProjectLogMapper;
import com.dci.dao.pms.project.ProjectManagerMapper;
import com.dci.model.project.ProjectLog;
import com.dci.model.project.ProjectManager;
import com.dci.result.JsonData;
import com.dci.result.PageResult;
import com.dci.result.ResultInfo;
import com.dci.result.ResultUtil;
import com.dci.service.project.ProjectLogService;
import com.dci.util.Constant;
import com.dci.util.UUIDBuild;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @version V1.0
 * @Package com.dci.service.project
 * @Description: 项目管理修改 记录接口
 * @author: 李振振
 * @date: 2019/7/11
 */
@Service
public class ProjectLogServiceImpl implements ProjectLogService {

    @Resource
    private ProjectLogMapper projectLogMapper;

    @Resource
    private ProjectManagerMapper projectManagerMapper;
    /**
     * 动态新增记录信息
     *
     * @param record
     * @return JsonData 封装数据
     */
    @Override
    public JsonData insertSelective(ProjectLog record) {
        record.setId(UUIDBuild.getUUID());
        record.setUpdateTime(new Date());
        int num = projectLogMapper.insertSelective(record);
        if(num > 0){
            return JsonData.success(906);
        } else {
            return JsonData.success( 911);
        }
    }

    /**
     * 动态修改项目信息
     *
     * @param projectManager 项目信息
     * @return ResultInfo 封装返回值
     */
    @Override
    public ResultInfo updateProject(ProjectManager projectManager){
        ProjectManager projectManager1 = projectManagerMapper.selectByPrimaryKey(projectManager);
        String updateKeys = "";
        String updateBefore = "";
        String updateAfter = "";
        int num1 = 0;
        // 获取当前系统时间和日期并格式化输出:
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat(Constant.FORMATDAY);
        // 格式转换.String
        String dateTime = df.format(date);
        projectManager.setUpdateTime(dateTime);
        // 项目信息修改
        int num = projectManagerMapper.updateByPrimaryKeySelective(projectManager);
        if(num > 0) {
            ProjectManager projectManager2 = projectManagerMapper.selectByPrimaryKey(projectManager);
            try {
                if(projectManager1 != null && projectManager2 != null ){
                    Class cls = projectManager1.getClass();
                    Field[] fields = cls.getDeclaredFields();
                    Class cls2 = projectManager2.getClass();
                    Field[] fields2 = cls2.getDeclaredFields();
                    // 遍历比对修改前后信息
                    for(int i=0; i<fields.length; i++){
                        Field f = fields[i];
                        Field f2 = fields[i];
                        f.setAccessible(true);
                        f2.setAccessible(true);
                        if(f.getName().equals(f2.getName())){
                            // 根据值是否修改 拼接字符串 以"~" 分割
                            if(f.get(projectManager1) != null && f2.get(projectManager2) != null){
                                if(!f.get(projectManager1).equals(f2.get(projectManager2))){
                                    if(updateKeys.equals("")){
                                        updateKeys += f.getName();
                                        updateBefore += f.get(projectManager1);
                                        updateAfter += f2.get(projectManager2);
                                    } else {
                                        updateKeys = updateKeys + Constant.SPLIT_WAVE + f.getName();
                                        updateBefore = updateBefore + Constant.SPLIT_WAVE + f.get(projectManager1);
                                        updateAfter = updateAfter + Constant.SPLIT_WAVE + f2.get(projectManager2);
                                    }
                                }
                            }
                        }

                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            // 根据是否修改内容 添加记录
            if(!updateKeys.equals("")) {
                ProjectLog projectLog = ProjectLog.builder().id(UUIDBuild.getUUID())
                        .projectId(projectManager.getId())
                        .updateKeys(updateKeys)
                        .updateBeforeValues(updateBefore)
                        .updateAfterValues(updateAfter)
                        .updateTime(date)
                        .userId(projectManager.getUpdateUserId())
                        .userName(projectManager.getUpdateUser())
                        .updateType(Constant.TYPEUPDATE)
                        .build();
                num1 = projectLogMapper.insertSelective(projectLog);
            }
        }
        if (num1 > 0) {
            return ResultUtil.createSuccess(Config.MESSAGE, 906, null);
        } else {
            return ResultUtil.createSuccess(Config.MESSAGE, 911, null);
        }

    }

    /**
     * 根据条件查询 修改记录
     *
     * @param record 记录信息
     * @return PageResult 封装返回值
     */
    @Override
    public PageResult queryProjectLogList(ProjectLog record) {
        // 分页插件
        // PageHelper.startPage( pageNum, pageSize );
        List<ProjectLog> projectLogs = projectLogMapper.queryProjectLogList(record);
        PageInfo<ProjectLog> pageInfo = new PageInfo<ProjectLog>( projectLogs );
        Long count = pageInfo.getTotal();
        return PageResult.success(projectLogs,null,"906",count);
    }

    /**
     * 根据条件分页查询 修改记录
     *
     * @param record 记录信息
     * @return PageResult 封装返回值
     */
    @Override
    public PageResult queryProjectLogPageList(ProjectLog record ,Integer pageNum ,Integer pageSize) {
        // 分页插件
        PageHelper.startPage( pageNum, pageSize );
        List<ProjectLog> projectLogs = projectLogMapper.queryProjectLogList(record);
        PageInfo<ProjectLog> pageInfo = new PageInfo<ProjectLog>( projectLogs );
        Long count = pageInfo.getTotal();
        return PageResult.success(projectLogs,null,"906",count);
    }

    /**
     * 根据主键删除提示记录
     * @param object 主键id
     * @return ResultInfo 封装数据
     */
    @Override
    public ResultInfo deleteByPrimaryKey(String object ) {
        Map maps = (Map) JSON.parse( object );
        String[] list = JSONArray.parseArray( maps.get( "list" ).toString() ).toArray( new String[0] );
        for (int i = 0; i < list.length; i++) {
            String id = list[i];
            projectLogMapper.deleteByPrimaryKey( id );
        }
        return ResultUtil.createSuccess( Config.MESSAGE, 906, null );
    }

    /**
     * 更新修改记录
     * @param record 记录信息
     * @return ResultInfo 封装数据
     */
    @Override
    public ResultInfo updateProjectLog(ProjectLog record) {
        record.setUpdateTime(new Date());
        int num = projectLogMapper.updateByPrimaryKeySelective(record);
        if( num>0 ){
            return ResultUtil.createSuccess( Config.MESSAGE, 906, null );
        }
        return ResultUtil.createSuccess( Config.MESSAGE, 911, null );
    }
}
